import java.util.HashMap;
import java.util.Map;

public class Solution13 {
    //873. 最长的斐波那契子序列的长度
    public int lenLongestFibSubseq(int[] arr) {
        Map<Integer,Integer> hash = new HashMap<>();
        int n = arr.length;
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            hash.put(arr[i],i);
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = 2;
            }
        }
        int ret = 0;
        for (int j = 2; j < n; j++) {
            for (int i = 1; i < j; i++) {
                int a = arr[j] - arr[i];
                if (a < arr[i] && hash.containsKey(a)) {
                    dp[i][j] = dp[hash.get(a)][i] + 1;
                    ret = Math.max(ret,dp[i][j]);
                }
            }
        }
        return ret;
    }
}
